﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TMT.Data.Service;
using TMT.Data.Common;
using TMT.Data.Entities;

namespace TMT.Data.Base
{
    public class PageService : IPageService
    {
        #region Singleton
        private static IPageService _instance;
        public static IPageService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new PageService();
                }

                return _instance;
            }
        }
        #endregion

        #region Declaration
        private TMTDataEntitiesDataContext _dataContext;
        #endregion

        #region Constructor
        public PageService()
        {
            _dataContext = new TMTDataEntitiesDataContext();
        }
        #endregion

        #region IPageService Members

        public Common.OperationResult AddPage(Page page)
        {
            OperationResult addResult = new OperationResult()
            {
                OperationName = "AddPage",
                Result = false,
                Message = string.Empty
            };

            try
            {
                _dataContext.Pages.InsertOnSubmit(page);
                _dataContext.SubmitChanges();
                addResult.Result = true;
                addResult.ReturnObject = page;
            }
            catch (Exception ex)
            {
                addResult.Result = false;
                addResult.Message = ex.Message;
                addResult.ReturnObject = page;
            }

            return addResult;
        }

        public Common.OperationResult UpdatePage(Page page)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Delete page by pageid
        /// </summary>
        /// <param name="pageId"></param>
        /// <returns></returns>
        public Common.OperationResult DeletePage(int pageId)
        {
            OperationResult addResult = new OperationResult()
            {
                OperationName = "DeletePage",
                Result = false,
                Message = string.Empty
            };

            try
            {
                Page deletedPage = _dataContext.Pages.Where(p => p.PageId == pageId).Single();
                if (deletedPage != null)
                {
                    _dataContext.Pages.DeleteOnSubmit(deletedPage);
                    _dataContext.SubmitChanges();
                }
                addResult.Result = true;
                addResult.ReturnObject = pageId;
            }
            catch (Exception ex)
            {
                addResult.Result = false;
                addResult.Message = ex.Message;
                addResult.ReturnObject = pageId;
            }

            return addResult;
        }

        public IList<TMTPage> GetAllPage()
        {
            IList<Page> pages = _dataContext.Pages.ToList();
            IList<TMTPage> retList = new List<TMTPage>();
            for (int i = 0; i < pages.Count; i++)
			{
                retList.Add(pages[i].ToTMTPage());
			}
            return retList;
        }

        public Page GetPageById(int pageId)
        {
            return _dataContext.Pages.Where(p => p.PageId == pageId).Single();
        }
        #endregion
       
    }
}
